package com.imusic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class UIApplication {

  private static final Logger log = LoggerFactory.getLogger(UIApplication.class);
  @Autowired
  RestTemplate restTemplate;

  @RequestMapping("/start")
  public String start() throws InterruptedException {
    log.info("Calling Order Microservice");
    String response = restTemplate.getForObject("http://order-service/startOrder", String.class);
    Thread.sleep(100);
    log.info("Got response from Order Microservice [{}]", response);
    return response;
  }

  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
    return new RestTemplate();
  }


  public static void main(String[] args) throws Exception {
    SpringApplication.run(UIApplication.class, args);

  }
}
